package com.study.minio.utils;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * @author Mayjean
 * @date 2020/8/17 17:55
 */
public class DateUtils {


    /**
     * 日期转换
     *
     * @param timestamp 13位时间戳
     * @param pattern   ： yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static String format(long timestamp,String pattern){
        return format(new Date(timestamp),pattern);
    }

//    public static String format(long timestamp, String pattern) {
//        DateTimeFormatter dtf = DateTimeFormatter.ofPattern(pattern);
//        return dtf.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneId.systemDefault()));
//    }

    public static String format(Date date,String pattern){
        Instant instant = date.toInstant();
        ZoneId zone = ZoneId.systemDefault();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
        return localDateTime.format(DateTimeFormatter.ofPattern(pattern));
    }

    /**
     * 通过LocalDateTime获取当前格式化时间
     *
     * @param pattern :"yyyy-MM-dd"
     * @return
     */
    public static String format(String pattern) {
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern(pattern));
    }

    /**
     * 传入Long类型时间戳，返回传入的String日期格式
     * @param time
     * @param examString
     * @return
     */
    public static String getDetailTime(Long time, String examString) {
        return new SimpleDateFormat(examString).format(time);
    }

    /***
     * 传入String类型格式化时间，转换成Long类型的时间戳
     * jdk8的bug  无法转义yyyyMMddHHmmssSSS的日期格式 需要自己兼容
     * @param strTime
     * @param pattern:yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static long parse(String strTime, String pattern) {
        LocalDateTime parse = LocalDateTime.parse(strTime, DateTimeFormatter.ofPattern(pattern));
        return LocalDateTime.from(parse).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
    }

    public static Date getExpectDate(String dateStr,int addYear, int monthAdd, int dayAdd) {
        return getExpectDate(Integer.parseInt(dateStr.substring(0,4))
                ,Integer.parseInt(dateStr.substring(4,6))-1
                ,Integer.parseInt(dateStr.substring(6,8))
        ,addYear, monthAdd, dayAdd);
    }

    public static Date getExpectDate(int year, int month, int day,int addYear, int monthAdd, int dayAdd) {
        Calendar cal = Calendar.getInstance();
        cal.set(year,month,day);
        cal.add(Calendar.YEAR,addYear);
        cal.add(Calendar.MONTH, monthAdd);
        cal.add(Calendar.DAY_OF_MONTH, dayAdd);
        return cal.getTime();
    }

    public static Date getExpectDate(int year, int month, int day,int hourOfDay,int minute,int seconds,int addYear, int monthAdd, int dayAdd) {
        Calendar cal = Calendar.getInstance();
        cal.set(year,month,day,hourOfDay,minute,seconds);
        cal.add(Calendar.YEAR,addYear);
        cal.add(Calendar.MONTH, monthAdd);
        cal.add(Calendar.DAY_OF_MONTH, dayAdd);
        return cal.getTime();
    }

    /**获取期望日期 从当前日期算**/
    public static String getExpectDate(String dateStr,int addYear, int monthAdd, int dayAdd,String partener) {
        Date date = getExpectDate(dateStr,addYear, monthAdd, dayAdd);
        return new SimpleDateFormat(partener).format(date);
    }
    /**获取期望日期 **/
    public static long getExpectDate(int addYear, int monthAdd, int dayAdd,long inTimeMillis) {
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(inTimeMillis);
        cal.add(Calendar.YEAR,addYear);
        cal.add(Calendar.MONTH, monthAdd);
        cal.add(Calendar.DAY_OF_MONTH, dayAdd);
        return cal.getTimeInMillis();
    }
    public static String getFirstDayNextMonth() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.MONTH, 1);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        Date date = cal.getTime();
        return new SimpleDateFormat("yyyy-MM-dd").format(date);
    }

    public static List<String> getBetweenDays(String startyyyyMMdd, String endyyyyMMdd) throws ParseException {
//        if (StringUtils.isEmpty(startTime) || StringUtils.isEmpty(endTime)) {
//            return null;
//        }
        //1、定义转换格式
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");

        Date start = df.parse(startyyyyMMdd);
        Date end = df.parse(endyyyyMMdd);
        if (Objects.isNull(start) || Objects.isNull(end)) {
            return null;
        }
        List<String> result = new ArrayList();

        Calendar tempStart = Calendar.getInstance();
        tempStart.setTime(start);

        tempStart.add(Calendar.DAY_OF_YEAR, 1);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        Calendar tempEnd = Calendar.getInstance();
        tempEnd.setTime(end);
        result.add(sdf.format(start));
        while (tempStart.before(tempEnd)) {
            result.add(sdf.format(tempStart.getTime()));
            tempStart.add(Calendar.DAY_OF_YEAR, 1);
        }
        return result;
    }

    /**
     * 获取两个月份之间的所有月
     * @param minDate  202101
     * @param maxDate  202105
     * @return
     */
    public static List<String> getMonthBetween(String minDate, String maxDate){
        ArrayList<String> result = new ArrayList();
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");//格式化为年月

            Calendar min = Calendar.getInstance();
            Calendar max = Calendar.getInstance();
            min.setTime(sdf.parse(minDate));
            min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);

            max.setTime(sdf.parse(maxDate));
            max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);

            Calendar curr = min;
            while (curr.before(max)) {
                result.add(sdf.format(curr.getTime()));
                curr.add(Calendar.MONTH, 1);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return result;
    }
}
